#include "updatearticlecontentresponse.h"
#include "newspaper/article.h"

#include <QDebug>

UpdateArticleContentResponse::UpdateArticleContentResponse(Article* article, QObject *parent)
    :JSONResponse(parent),
    m_article(article),
    m_checkReplyDataSuccess(false)

{
}

// ---------------------------------------------------------------------------------
Network::Response::Type UpdateArticleContentResponse::type() const
{
    return Response::UpdateArticleContent;
}

// ---------------------------------------------------------------------------------
void UpdateArticleContentResponse::parse(const QVariant &data)
{
    //qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }

    QVariantMap map = data.toMap();

    if (!map.contains("linfo")) {
        setError(Response::Property, "The \"linfo\" value missing");
        return;
    }
    QVariant linfo = map.value("linfo");
    if (linfo.type() != QVariant::Map) {
        setError(Response::Property, "The \"linfo\" value isn't competitived");
        return;
    }


    QVariantMap mapchild = linfo.toMap();

    if (!parseArticleContent(mapchild)) {
        return;
    }

    if (!parseArticleListImage(mapchild)) {
        return;
    }

    m_checkReplyDataSuccess = true;

}

// ---------------------------------------------------------------------------------
bool UpdateArticleContentResponse::parseArticleContent(const QVariantMap &map)
{

    if (!map.contains("content")) {
        setError(Response::Property, "The \"content\" value missing");
        return false;
    }
    QVariant content = map.value("content");
    if (content.type() != QVariant::String) {
        setError(Response::Property, "The \"content\" value isn't competitived");
        return false;
    }

    m_article->setContent(content.toString());
    m_article->setIsHasContent(true);

    return true;
}

// ---------------------------------------------------------------------------------
bool UpdateArticleContentResponse::parseArticleListImage(const QVariantMap &map)
{

    if (!map.contains("listimage")) {
        setError(Response::Property, "The \"listimage\" value missing");
        return false;
    }
    QVariant listimage = map.value("listimage");
    if (listimage.type() != QVariant::List) {
        setError(Response::Property, "The \"listimage\" value isn't competitived");
        return false;
    }

    QStringList listArticleImage = listimage.toStringList();

    m_article->setListLinkImage(listArticleImage);

    return true;
}

